package com.zhdl.modules.common.production.service.impl;

import com.project.common.service.impl.BaseServiceImpl;
import com.zhdl.modules.common.production.entity.ProductionRecord;
import com.zhdl.modules.common.production.repository.ProductionRecordRepository;
import com.zhdl.modules.common.production.service.ProductionRecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.Instant;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class ProductionRecordServiceImpl extends BaseServiceImpl<Integer, ProductionRecord, ProductionRecordRepository> implements ProductionRecordService {

    @Resource
    private ProductionRecordRepository productionRecordRepository;

    // 根据季度编号返回该季度中所有月份的数组
    public static String[] getMonthsInQuarter(String year, int quarter) {
        // 检查季度编号是否在有效范围内
        if (quarter < 1 || quarter > 4) {
            throw new IllegalArgumentException("季度编号必须在1到4之间");
        }
        // 根据季度编号计算月份范围并返回数组
        int startMonth = (quarter - 1) * 3 + 1; // 计算季度的第一个月份
        String[] months = new String[3]; // 每个季度有3个月份
        for (int i = 0; i < 3; i++) {
            months[i] = String.format("%02d", startMonth + i); // 填充月份数组
            months[i] = year + "-" + months[i];
        }
        return months;
    }

    public static Map<String, Integer> getProductionByTime(List<ProductionRecord> list) {
        Map<String, Integer> map = new HashMap<>();

        LocalDate currentDate = LocalDate.now();    // 获取当前日期
        DateTimeFormatter formatterYear = DateTimeFormatter.ofPattern("yyyy");    // 定义日期格式
        String year = currentDate.format(formatterYear);

        for (int i = 1; i <= 12; i++) {
            String month = year + "-" + String.format("%02d", i);
            map.put(month, 0);
            for (ProductionRecord productionRecord : list) {
                if (productionRecord.createDate.contains(month)) {
                    map.put(month, map.get(month) + productionRecord.getProductionOutput());
                }
            }
        }
        return map;
    }

    public static Integer getProductionByAll(List<ProductionRecord> list) {
        Integer total = 0;
        for (ProductionRecord productionRecord : list) {
            total += productionRecord.getProductionOutput();
        }
        return total;
    }

    public void saveProductionRecord(Integer formulaId, Integer productionOutput) {
        ProductionRecord productionRecord = new ProductionRecord();
        productionRecord.setFormulaId(formulaId);
        productionRecord.setProductionOutput(productionOutput);
        productionRecord.setStartTime(Instant.now().toEpochMilli());
        productionRecord.setEndTime((long) -999);
        productionRecordRepository.save(productionRecord);
    }

    public void updateProductionRecord(Integer formulaId, Integer productionOutput) {
        List<ProductionRecord> byFormulaId = productionRecordRepository.findByFormulaId(formulaId);
        byFormulaId.forEach(byFormulaIdFor -> {
            if (byFormulaIdFor.getEndTime() == -999) {
                byFormulaIdFor.setProductionOutput(productionOutput);
                productionRecordRepository.save(byFormulaIdFor);
            }
        });
    }

    public void endProductionRecord(Integer formulaId) {
        //查询是否有-999 如果有则先结束

        //然后新建一条当前的记录
        saveProductionRecord(formulaId, 1);
    }
}
